package com.sxpi.zixun.service.resources;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.zixun.bo.ResourcesBo;
import com.sxpi.zixun.common.utils.fn.asserts.Vsserts;
import com.sxpi.zixun.mapper.ResourcesMapper;
import com.sxpi.zixun.pojo.Resources;
import com.sxpi.zixun.vo.ResourcesVo;
import org.springframework.stereotype.Service;

/**
 * @Author: 董伟豪
 * @Date: 2022/9/2 19:47
 */
@Service
public class IResourcesServiceImpl extends ServiceImpl<ResourcesMapper, Resources> implements IResourcesService {

    /**
     * 查询资源库列表信息
     * @param resourcesVo
     * @return
     */
    @Override
    public IPage<ResourcesBo> findResourcesPage(ResourcesVo resourcesVo) {
        // 设置分页信息
        Page<Resources> page = new Page<>(resourcesVo.getPageNo(), resourcesVo.getPageSize());
        // 设置查询条件
        LambdaQueryWrapper<Resources> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 根据关键词搜索信息
        lambdaQueryWrapper.like(Vsserts.isNotEmpty(resourcesVo.getKeyword()), Resources::getFilename, resourcesVo.getKeyword());
        // 根据分类id查询对应的资源库信息
        lambdaQueryWrapper.eq(Vsserts.isNotNull(resourcesVo.getCategoryId()), Resources::getResourceId, resourcesVo.getCategoryId());
        // 查询出未删除
        lambdaQueryWrapper.eq( Resources::getIsdelete,0);
        // 根据时间排降序
        lambdaQueryWrapper.orderByDesc(Resources::getCreateTime);
        // 查询分页返回
        IPage<Resources> results = this.page(page, lambdaQueryWrapper);
        return tranferPageBo(results, ResourcesBo.class);
    }

    /**
     * 统计分类下是否存在资源信息
     * @param cid
     * @return
     */
    @Override
    public int countResources(Long cid) {
        LambdaQueryWrapper<Resources> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Resources::getResourceId,cid);
        return this.count(lambdaQueryWrapper);
    }
}
